function affine_flow_demo(X, ts)
%AFFINE_FLOW_DEMO Simulates a flow on a rectangle
%
%   AFFINE_FLOW_DEMO(X, ts);
%
%       Shows the action results of a flow on a rectangle. 
%   
%       Input arguments:
%       - X:        The Lie algebraic matrix [2 x 3]
%       - ts:       The sequence of time steps at which the resultant
%                   rectangles are shown.
%

% Created by Dahua Lin, on April 4, 2012
%

%% generate transform sequences

Ts = affine_flow(X, ts);

%% generate transformed rectangles

n = length(ts);

rc0 = [-1 1 1 -1 -1; 1 1 -1 -1 1];
rcs = zeros([size(rc0), n]);

for i = 1 : n  
    T = Ts(:,:,i);
    A = T(1:2, 1:2);
    b = T(1:2, 3);
    
    rcs(:,:,i) = bsxfun(@plus, A * rc0, b);    
end

%% visualize the rectangles

cr_begin = [1 0.9 0.9];
cr_last = [1 0 0];

figure;

for i = 1 : n
    
    r = i / n;    
    cr = (1 - r) * cr_begin + r * cr_last;
    
    rc = rcs(:,:,i);
    
    hold on; 
    plot(rc(1,:), rc(2,:), '-', 'Color', cr);    
end

% determine plot range

xs = rcs(1,:);
ys = rcs(2,:);

xmin = min(xs);
xmax = max(xs);
ymin = min(ys);
ymax = max(ys);

x0 = xmin - (xmax - xmin) * 0.1;
x1 = xmax + (xmax - xmin) * 0.1;
y0 = ymin - (ymax - ymin) * 0.1;
y1 = ymax + (ymax - ymin) * 0.1;

axis([x0 x1 y0 y1]);
axis equal;



